<?php
/**
 * ==============================================
 * Copy right 2015-2016 http://wei.meididi88.com
 * ----------------------------------------------
 * This is not a free software, without any authorization is not allowed to use and spread.
 * ==============================================
 * @author: hy
 * @date: 2015-9-10
 * @version: v1.0.0
 */
namespace Common\Model;

use Think\Model;

/**
 * 服务项目 Model模型类
 */
class ServiceItemModel extends Model{
	
	protected $_validate = array(
		array('service_name','require','服务名必须填写！'),
		array('service_time','require','服务时长必须填写！'),
		array('effect','require', '功效必须填写！'),
		array('service_desc','require','服务简介必须填写！'),
		array('people_use','require', '适用人群必须填写！'),
		array('note','require', '注意事项必须填写！'),
		array('price','require', '原件价格必须填写！'),
	);
	
	protected $_auto = array(
		array('created','time',1,'function'), //新增时
	);
	
	/**
	 * 添加服务
	 * 
	 * @param array $data
	 * @return boolean
	 */
	public function addService($data) {
		$this->startTrans();
		$autoModel = D('AutoId');
		$id = $autoModel->createAutoId(array('type' => 'service_item'));
		 
		$time = time();
		
		$addData ['id'] = $id;
		$addData ['service_name'] = $data ['service_name'];
		$addData ['service_time'] = $data ['service_time'];
		$addData ['service_cover'] = $data ['service_cover'];
		$addData ['order_banner'] = $data ['order_banner'];
		$addData ['effect'] = $data ['effect'];
		$addData ['service_desc'] = $data ['service_desc'];
		$addData ['people_use'] = $data ['people_use'];
		$addData ['note'] = $data ['note'];
		$addData ['original_price'] = $data ['original_price'];
		$addData ['tag'] = $data ['tag'];
		$addData ['tag_bg'] = $data ['tag_bg'];
		
		if (isset($data ['price']) && $data ['price'] != 0) {
			$addData ['price'] = $data ['price'];
		} else {
			$addData ['price'] = $addData ['original_price'];
		}
		
		if (isset($data['tag']) && $data ['tag']) {
			$addData ['tag'] = $data ['tag'];
		}
		
		$addData ['created'] = $time;
		
		if (!$this->add($addData)) {
			$this->error = '创建服务项目失败！';
			$this->rollback();
			return false;
		}
		
		$titles = I('post.title');
		$contents = I('post.content');
		
		$upload_number = I('post.number');
		$covers = I('post.cover_img_name');
		
		$products = i('post.product');
		if ($products) {
			$ProductModel = M('service_use_product');
			foreach ($products as $key => $product) {
				$ProductModel->add(array(
					'target_id' => $id,
					'product_id' => $product,
					'created' => $time,
				));
			}
		}
		
		if ($covers) {
			$PhotoModel = D('Photo');
			$csort = 0;
			foreach ($covers as $key => $cover) {
				++$csort;
				$photoData = array(
					'target_id' => $id,
					'img_name' => $cover,
					'type' => 'cover',
					'sort' => $csort,
					'upload_number' => $upload_number,
					'created' => $time
				);
				$addResult = $PhotoModel->add($photoData);
				if (!$addResult) {
					$this->rollback();
					$this->error = '添加服务项目展示图片失败';
					return false;
				}
			}
		}
		 
		if ($titles) {
			$ContentModel = D('Content');
			foreach ($titles as $key => $title) {
				$contentData = array(
					'target_id' => $id,
					'title' => $title,
					'content' => $contents [$key],
					'created' => $time
				);
				$stepAddResult = $ContentModel->add($contentData);
				if (!$stepAddResult) {
					$this->rollback();
					$this->error = '添加步骤失败';
					return false;
				}
			}
		}
		
		S('Service_Item_List', NULL);
		$this->commit();
		return $id;
	}
	
	/**
	 * 清空缓存
	 * @param string $id
	 */
	public function clearCache($id = false) {
		$cacheKey = $this->getCacheKey($id);
		
		S($cacheKey, null);
	}
	
	/**
	 * 获取缓存Key
	 *
	 * @param string $id	服务项目编号
	 * @return string
	 */
	public function getCacheKey($id = false) {
		return $id?'Service_Item_'. $id:'Service_Item_List';
	}
	
	/**
	 * 获取服务项目列表
	 *
	 * @return array|boolean
	 */
	public function getServiceList() {
// 		$cacheKey = $this->getCacheKey();
// 		$list = S($cacheKey);
// 		if (!$list) {
			$list = $this->where(array('status' => 1))->select();
// 			S($cacheKey, $list);
// 		}
	
		return $list;
	}
	
	/**
	 * 获取服务项目的销量
	 * 
	 * @param number $id
	 * @return number
	 */
	public function getServicePurchasesTotal($id) {
		$total = $this->field('purchases_total')->where(array('id' => $id))->find();
		
		return $total?$total ['purchases_total']:0;
	}
	
	/**
	 * 读取服务信息
	 *
	 * @param number $id	服务项目编号
	 * @return boolean|array
	 */
	public function getServiceInfo($id) {
// 		$cacheKey =$this->getCacheKey($id);
// 		$info = S($cacheKey);
// 		if (!$info) {
			$info = $this->where(array('id' => $id))->find();
// 			S($cacheKey, $info);
// 		}
	
		return $info;
	}
	
	/**
	 * 保存编辑
	 * 
	 * @param array $data
	 * @return boolean
	 */
	public function updateInfo($data) {
		$this->startTrans();
		$id = $data ['id'];
		
		$updateData ['service_name'] = $data ['service_name'];
		$updateData ['service_time'] = $data ['service_time'];
		$updateData ['service_cover'] = $data ['service_cover'];
		$updateData ['order_banner'] = $data ['order_banner'];
		$updateData ['effect'] = $data ['effect'];
		$updateData ['service_desc'] = $data ['service_desc'];
		$updateData ['people_use'] = $data ['people_use'];
		$updateData ['note'] = $data ['note'];
		$updateData ['original_price'] = $data ['original_price'];
		$updateData ['tag'] = $data ['tag'];
		$updateData ['tag_bg'] = $data ['tag_bg'];
		
		if (isset($data ['price']) && $data ['price'] != 0) {
			$updateData ['price'] = $data ['price'];
		} else {
			$updateData ['price'] = $updateData ['original_price'];
		}
		
		if (isset($data['tag']) && $data ['tag']) {
			$updateData ['tag'] = $data ['tag'];
		}
		
		$item = $this->getServiceInfo($id);
		
		unset($item ['id']);
		unset($item ['created']);
		unset($item ['status']);
		unset($item ['purchases_total']);
		
		$hasModify = array_diff($updateData, $item);
		if ($hasModify && !$this->where(array('id' => $id))->save($hasModify)) {
			$this->error = '编辑服务项目失败！';
			$this->rollback();
			return false;
		}
		
		$covers = I('post.cover_img_name');
		$number = I('post.number');
		
		$ProductUseModel = D('ServiceUseProduct');
		$products = i('post.product');
		
		$editUseProductResult = $ProductUseModel->editUseProduct($id, $products);
		if (!$editUseProductResult) {
			$this->error = '编辑服务使用产品失败！';
			$this->rollback();
			return false;
		}
		
		$PhotoModel = D('Photo');
		$PhotoModel->type = 'carousel';
		$covers = $covers?$covers: array();
		$editCoverResult = $PhotoModel->editTargetPhotos($id, $covers, $number);
		if (!$editCoverResult) {
			$this->error = '编辑服务轮播图片失败！';
			$this->rollback();
			return false;
		}
		
		$ContentModel = D('Content');
		$ids = $data ['step_id'];
		$editResult = $ContentModel->editContent($id, $ids, $data ['content'], $data ['title']);
		if (!$editResult) {
			$this->error = '编辑服务项目步骤失败！';
			$this->rollback();
			return false;
		}
		
		if ($hasModify) {
			S('Service_Item_'.$id, NULL);
			S('Service_Item_List', NULL);
		}
		
		$this->commit();
		return true;
	}
}